#!/usr/bin/env python
# -*- coding: utf-8 -*-



# 运行前注意安装:
# pip install baidu-aip 
import sys
import time
import socket
import json
import base64
import requests
from datetime import datetime
print(datetime.now())
print(sys.version)

domain = "aip.baidubce.com"
myaddr = socket.getaddrinfo(domain,'https')
print(str(domain) + " = " + myaddr[0][4][0])

start = time.time()
appid = '22421544'
client_id = 'BWNu4mjd2G9vS4bWNcHAzVIt'
client_secret = '3wCsz0CSy3XCUBoF5dNh6diGCBbjpBmZ'
print("appid:" + appid)
print("client_id:" + client_id)
print("client_secret:" + client_secret)

host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials'
host += "&client_id=%s&client_secret=%s" % (client_id, client_secret)

session = requests.Session()
response = session.get(host)
access_token = response.json().get("access_token")
print("access_token:" + access_token)

request_url = "https://aip.baidubce.com/rpc/2.0/nlp/v2/dnnlm_cn?charset=UTF-8&access_token=" + access_token

headers = {
    'Content-Type': 'application/json'
}
params = {
            "text": "刚才我说了什么来着"
}
params = json.dumps(params)
request_url = request_url + "?access_token=" + access_token
response = session.post(request_url, headers=headers, data=params)
content = response.content.decode('UTF-8')
print(content)

end = time.time()
print('Running time: %1.2f Seconds'%(end-start))